var base = require('utils/base.js');//引入消息提醒暴露的接口  

App({
  addListener: function (callback) {//跨页面刷新
    this.callback = callback;
  },
  setChangedData: function () {
    this.callback();
  },
  getUserInfo: function (cb,refresh) {
    var that = this
    if(!refresh){ //是否强制刷新
      if (wx.getStorageSync('memberData')){
        //有缓存
        cb(wx.getStorageSync('memberData'), true)
      }
      that.getUserInfo(cb,true)
    }else{
       //调用登录接口
      wx.login({
        success: function (login_res) {
           console.log('login1:'+JSON.stringify(login_res))
          wx.getUserInfo({
            success: function (res) {
              console.log('login2:'+JSON.stringify(res))
              //服务器请求用户登录数据

              var post_data = {
                  code: login_res.code,
                  encryptedData: res.encryptedData,
                  iv: res.iv,
                  body:'jamyo'
                }
              console.log(post_data)
             
                //登陆接口
           base.getJson('Member/login',post_data,function(data){
             console.log('登录成功')
             console.log(data)
             wx.setStorageSync('authorization', data.Authorization)
                  //用户信息接口
                  base.getJson('Member/memberInfo',null,function(data){
                      console.log(data)
                      wx.setStorageSync('memberData', data.member)
                       cb(data, true)
                  })
           })

            },
            fail: function (login_res) {
              console.log('登录失败' + login_res)
              cb(null, false)
            }
          })
        }
      })
    }
    
  }
})